﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransBatchDetailBase.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-27
//    @Email			:    445267987@qq.com
//    @Module           :    NFTransBatchDetailBase
//
// -------------------------------------------------------------------------

#include "NFTransBatchDetailBase.h"

#include <NFSnsRoleDetailService.h>
#include <NFTransLoadRoleDetail.h>
#include <NFSnsServer/NFSnsCommon/NFSnsRoleSimpleService.h>
#include "NFSnsServer/NFSnsCommon/NFSnsRoleSimple.h"

#include "NFLogicCommon/NFBaseDefine.h"
#include "NFServerComm/NFServerCommon/NFCacheDataMgr.h"
#include "NFSnsServer/NFSnsCommon/NFTransLoadRoleSimple.h"

NFTransBatchDetailBase::NFTransBatchDetailBase()
{
    if (NFShmMgr::Instance()->GetCreateMode() == EN_OBJ_MODE_INIT)
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransBatchDetailBase::~NFTransBatchDetailBase()
{
}

int NFTransBatchDetailBase::CreateInit()
{
    m_iServerType = NF_ST_SNS_SERVER;
    return 0;
}

int NFTransBatchDetailBase::ResumeInit()
{
    return 0;
}

int NFTransBatchDetailBase::QueryRole(int iRason)
{
    int iRetCode = 0;
    auto pRoleSimple = NFSnsRoleSimpleService::GetRoleSimple(m_ullRoleId);
    if (pRoleSimple == nullptr)
    {
        iRetCode = GetCacheDataSingleReq<NFTransLoadRoleSimple>(m_ullRoleId, iRason, E_TABLE_SNS_ROLE_SIMPLE);
        CHECK_ERR(m_ullRoleId, iRetCode, "GetRoleSimpleInfoBatchReq fail");
    }
    else
    {
        auto pRoleDetail = NFSnsRoleDetailService::GetRoleDetail(m_ullRoleId);
        if (pRoleDetail == nullptr)
        {
            iRetCode = GetCacheDataSingleReq<NFTransLoadRoleDetail>(m_ullRoleId, iRason, E_TABLE_SNS_ROLE_DETAIL);
            CHECK_ERR(m_ullRoleId, iRetCode, "GetRoleDetailInfoBatchReq fail");
        }
    }
    return 0;
}

int NFTransBatchDetailBase::GetCacheDataRes(int ullId, int iTableId, int iReason, int32_t errCode, google::protobuf::Message* pMessage)
{
    if (iTableId == E_TABLE_SNS_ROLE_SIMPLE)
    {
        if (errCode != 0)
        {
            LOG_ERR(m_ullRoleId, errCode, "get sns role simple info roleId:{} errCode:{}", m_ullRoleId, errCode);
            SetFinished(errCode);
            return 0;
        }

        proto_ff::tbSnsRoleSimpleInfo* pTbSnsRoleSimple = dynamic_cast<proto_ff::tbSnsRoleSimpleInfo*>(pMessage);
        CHECK_NULL_WF(m_ullRoleId, pTbSnsRoleSimple, "dynamic_cast<tbSnsRoleSimpleInfo> failed, roleId:{}", m_ullRoleId);

        auto pSnsRoleSimple = NFCacheDataMgr::Instance()->AddCacheData<NFSnsRoleSimple>(pTbSnsRoleSimple);
        CHECK_NULL_WF(m_ullRoleId, pSnsRoleSimple, "AddCacheData failed, roleId:{}", m_ullRoleId);

        auto pRoleDetail = NFSnsRoleDetailService::GetRoleDetail(m_ullRoleId);
        if (pRoleDetail == nullptr)
        {
            int iRetCode = GetCacheDataSingleReq<NFTransLoadRoleDetail>(m_ullRoleId, 0, E_TABLE_SNS_ROLE_DETAIL);
            CHECK_ERR(m_ullRoleId, iRetCode, "GetRoleDetailInfoBatchReq fail");
        }
        else
        {
            SetFinished(0);
        }
    }
    else if (iTableId == E_TABLE_SNS_ROLE_DETAIL)
    {
        if (errCode != 0)
        {
            LOG_ERR(m_ullRoleId, errCode, "get sns role detail info failed roleId:{}");
            SetFinished(errCode);
            return 0;
        }

        proto_ff::tbSnsRoleDetail* pTbSnsRoleDetail = dynamic_cast<proto_ff::tbSnsRoleDetail*>(pMessage);
        CHECK_NULL_WF(m_ullRoleId, pTbSnsRoleDetail, "dynamic_cast<tbSnsRoleSimpleInfo> failed, roleId:{}", m_ullRoleId);

        auto pRoleDetail = NFSnsRoleDetailService::AddRoleDetail(m_ullRoleId, pTbSnsRoleDetail);
        CHECK_NULL(m_ullRoleId, pRoleDetail);

        LOG_ERR(m_ullRoleId, errCode, "get sns role detail info success, roleId:{}", m_ullRoleId);
        SetFinished(0);
        return 0;
    }

    SetFinished(-1);
    return 0;
}
